// pages/specialArea/motherAndBaby/motherAndBaby.js
import goodsApi from '../../../service/goodsApi';
const app = getApp()
const SUCCESS_CODE = app.globalData.codeConfig.errCode.SUCCESS_CODE

Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchKeyRecommend: '搜索',
        talentInfo: {},
        talentId: '',
        loading: false,
        loadingText: '加载中...',
        defaultShow: false,
        goodsList: [{
            "commission": "2",
            "goodsId": "100a5b2e79a95a5454134",
            "name": "益达口香糖",
            "picUrl": "http://dev-yopin-img.bumeng.cn/butalent/spu0002.png",
            "price": "12",
            "realPrice": "0.1",
            "recommend": "0"
        }],
        startPage: 1,
        pageSize: 50,
        goodsCategory: '1',
        categoryId: '1',
        loadTips: '上拉加载',
        upAbled: true, // 是否能上拉加载
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        wx.setNavigationBarTitle({
            title: options.name
        })
        this.setData({
            categoryId: options.type,
            talentInfo: wx.getStorageSync('talentInfo'),
            talentId: wx.getStorageSync('talentId'),
            searchKeyRecommend: app.globalData.searchKeyRecommend
        })
        if (options.type == '99') {
            this.setData({
                goodsCategory: '2'
            })
        }
        this.getGoodsList()
    },

    // 点击搜索框
    onGoSearch() {
        wx.navigateTo({
            url: '../../search/search'
        })
    },

    // 获取商品列表
    getGoodsList() {
        let that = this
        if (that.data.startPage === 1) {
            that.setData({
                loading: true,
                defaultShow: false,
                goodsList: []
            })
        }

        let params = {
            startPage: that.data.startPage,
            pageSize: that.data.pageSize,
            goodsCategory: that.data.goodsCategory,
            categoryId: that.data.categoryId,
        }
        goodsApi.getGoodsList(params).then(res => {
            if (res.data.errCode === SUCCESS_CODE) {
                let _goodsList = that.data.goodsList.concat(res.data.data.goodsList)
                that.setData({
                    loading: false,
                    goodsList: _goodsList
                })
                if (that.data.goodsList.length === 0) {
                    that.setData({
                        defaultShow: true,
                        upAbled: false
                    })
                } else {
                    that.setData({
                        defaultShow: false
                    })
                }
                if (res.data.data.goodsList.length < that.data.pageSize) {
                    that.setData({
                        loadTips: '暂无更多数据',
                        upAbled: false
                    })
                } else {
                    that.setData({
                        loadTips: '上拉加载'
                    })
                }
            } else {
                that.setData({
                    loading: false
                })
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
            }
        }).catch(res => {
            that.setData({
                loadingText: '系统内部错误'
            })
        })
    },
    
    // 点击商品券进入商品详情页
    toGoodsDetail(e) {
        let goodsId = e.currentTarget.dataset.goodsid
        wx.navigateTo({
            url: `../../goodsDetail/goodsDetail?goodsId=${goodsId}`
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        if (this.data.upAbled) {
            this.setData({
                startPage: this.data.startPage + 1,
                loadTips: '加载中...'
            })
            this.getGoodsList()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function(e) {
        if (e.from === 'button') {
            let data = e.target.dataset
            // 商品分享
            return {
                title: data.name,
                path: data.url,
                imageUrl: data.img
            }
        }
    }
})